; Macro file for Duex Users only
; Generated by Modix - Version 3.4.5 Macro X
; Use only after cerifying connection between both nozzles and puck. 

M300 S666 P666					; beep
M291 S3 R"Automatic Z Offset Calibration" P"Press OK to continue, or CANCEL to abort"

var errorfix_primary = 0.000	; constant offset
var errorfix_secondary = 0.000	; constant offset

G31 Z0							; set Z-offset to Z=0
echo >"0:/sys/config_probe.g" "G31 Z0"					; Set the primary Z-offset to 0 before starting
G10 P1 Z0						; set secondary Z-offset to Z=0
echo >"0:/sys/config_probe_secondary.g" "G10 P1 Z0"		; Set the secondary Z-offset to 0 before starting

M291 S3 R"Automatic Z Offset Calibration" P"Wait for the nozzle to reach printing temperatures"
M568 P0 S210 R210			; set tool 0 (primary hotend) temperature to 210c
M568 P1 S210 R210			; set tool 1 (secondary hotend) temperature to 210c

G4 P0						; wait for movements to have stopped

M116 P0 H5					; wait for temperature to reach within 5c of set temperature

G28							; home all
G31 Z0						; set Z-offset to Z=0
M564 H1 S0					; Negative movements are allowed
T0							; select tool 0 (primary hotend)
G29 S2						; cancel mesh bed compensation
G90 						; absolute positioning
G1 Z20						; go to Z20
M291 S3 R"Clean the primary hotend nozzle" P"The nozzle has been pre-heated. Clean it from any plastic debris"
G1 X30 Y150 F6000			; go to X20, Y150
G4 P0						; wait for movements to have stopped

M300 S666 P666				; beep
M291 S3 P"Place the offset tool below the nozzle and press OK to perform the macro, or CANCEL to abort"

G4 P200						; wait 200ms
G91							; relative moves
G1 H1 Z-5 F90				; move 5mm down, stop when hitting the offset tool (but don't risk crashing the bed)
G92 Z0						; set current position as Z=0
G4 P0						; wait for moves to complete
G1 Z2 X3 F200				; move 2mm up and 3mm to the right
G1 H1 Z-3 F30				; move 5mm down, stop when hitting the offset tool (but don't risk crashing the bed)
G92 Z0						; set current position as Z=0
G4 P200						; wait 200ms
G1 Z6 F200					; move up 11mm
G4 P0						; wait for moves to complete

G91							; relative moves
G1 X{sensors.probes[0].offsets[0] * -1} Y{sensors.probes[0].offsets[1] * -1} F1500	; move nozzle so that BLTouch is in it's position 
G90							; absolute moves

G30 S-3						; probes Z and adjusts the probe trigger height to match the actual stop height.
G4 P200						; wait 200ms
G91							; relative movement
M568 P0 S0					; set tool 0 temperatures to 0

var Z_auto_offset = {sensors.probes[0].triggerHeight+var.errorfix_primary}	; math to calculate the BLTouch offset
echo >"0:/sys/config_probe.g" "G31 Z"^{var.Z_auto_offset}			; save values to file

G91 						; absolute positioning
G1 Z5						; Move 5mm up
G4 P0						; wait for moves to complete

echo "wait for secondary hotend temperature to stabilize"
G1 H1 Y{(move.axes[1].max+5)*-1} F3000 	; Park T0
T1							; select the secondary hotend
G90							; absolute movement
G1 X50 Y150 F6000			; move secondary to X50 Y150
G91							; relative movement
M116 P1 H5					; wait for secondary temperature to reach within 5c of set temperature
G4 P0						; wait for movements to have stopped
M300 S666 P666				; beep
M291 S3 R"Clean the secondary hotend nozzle" P"The nozzle has been pre-heated. Clean it from any plastic debris"

M300 S666 P666				; beep
M291 S3 P"Place the offset tool below the nozzle and press OK to perform the macro, or CANCEL to abort"

G4 P200						; wait 200ms
G91							; relative moves
G1 H1 Z-10 F50				; move 10mm down, stop when hitting the offset tool (but don't risk crashing the bed)
G92 Z0						; set current position as Z=0
G4 P0						; wait for moves to complete
G1 Z3 X3 F200				; move 3mm up and 3mm to the right to avoid the previous probing spot
G1 H1 Z-5 F50				; move down 5mm, stop when hitting the offset tool (but don't risk crashing the bed)
G92 Z0
G4 P200						; wait 200ms
G1 Z11 F200					; move up 11mm
G4 P0						; wait for moves to complete
M568 P1 S0					; set tool 1 (secondary hotend) temperature to 0c

G1 H1 U{(move.axes[1].max+5)} F9000		; move secondary out of the way to home position

T0							; select primary without running a tool change script
G1 Y{sensors.probes[0].offsets[1] * -1 + 150} F3000	; move BLTouch into position
G90							; absolute movements
G30 S-3						; probes Z and adjusts the probe trigger height to match the actual stop height.
G4 P200						; wait 200ms
G91							; relative movements
G1 Z11						; move up 11mm

var Z_auto_offset_secondary = {sensors.probes[0].triggerHeight+var.errorfix_secondary-var.Z_auto_offset}    ; math to calculate the BLTouch offset
echo >"0:/sys/config_probe_secondary.g" "G10 P1 Z"^{var.Z_auto_offset_secondary}	; save values to file

G31 Z{var.Z_auto_offset}	; set primary Z-offset to the calibrated value
G10 P1 Z{var.Z_auto_offset_secondary}	; set secondary offset to the calibrated value

echo "the Z-offset has been stored . Remove the offset tool and place it back in the box"
echo "The primary Z-offset is", var.Z_auto_offset, "mm."
echo "The secondary Z-offset is", var.Z_auto_offset_secondary, "mm."
M291 S2 R"Z-offset has been stored" P"the values have been stored in the config_probe.g and config_probe_secondary.g files"

M564 S1 H1					; Negative movements are forbidden
M300 S666 P666				; beep
G28 Y						; Home Y axis
M568 P0 R0					; set tool 0 (primary hotend) standby temperature to 0
M568 P1 R0					; set tool 1 (secondary hotend) standby temperature to 0c
T-1 P0						; deselect all tools
M18							; release stepper motors